<?php

namespace common\widgets;

use Yii;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\i18n\Formatter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

/**
 * Excel Widget for generate Excel File or for load Excel File.
 *
 * @author Moh Khoirul Anam <moh.khoirul.anaam@gmail.com>
 * @copyright 2014
 * @since 1
 */
class Excel extends \moonland\phpexcel\Excel {

    /**
     * return default the first sheet index.
     */
    public $getOnlySheet = 0;

    /**
     * reading the xls file
     */
    public function readFile($fileName) {
        if (!isset($this->format)) {
            $this->format = IOFactory::identify($fileName);
        }
        $objectreader = IOFactory::createReader($this->format);
        $objectPhpExcel = $objectreader->load($fileName);
        $sheetCount = $objectPhpExcel->getSheetCount();
        if ($sheetCount <= 1) {
            return $this->getSheetData($objectPhpExcel);
        }

        $sheets = [];
        if (isset($this->getOnlySheet) && $this->getOnlySheet !== null) {
            if (is_array($this->getOnlySheet) && !empty($this->getOnlySheet)) {
                $sheets = $this->getOnlySheet;
            } else {
                if ($this->setIndexSheetByName) {
                    $objectPhpExcel->setActiveSheetIndexByName($this->getOnlySheet);
                } else {
                    $objectPhpExcel->setActiveSheetIndex($this->getOnlySheet);
                }
                return $this->getSheetData($objectPhpExcel);
            }
        }

        $sheetDatas = [];
        foreach ($objectPhpExcel->getSheetNames() as $sheetIndex => $sheetName) {
            $indexed = $sheetIndex;
            if ($this->setIndexSheetByName) {
                $objectPhpExcel->setActiveSheetIndexByName($sheetName);
                $indexed = $sheetName;
            } else {
                $objectPhpExcel->setActiveSheetIndex($sheetIndex);
            }
            if (!empty($sheets) && !in_array($indexed, $sheets)) {
                continue;
            }
            $sheetDatas[$indexed] = $this->getSheetData($objectPhpExcel, $indexed);
        }

        return $sheetDatas;
    }

    protected function getSheetData($objectPhpExcel, $indexed = false) {
        $sheetData = $objectPhpExcel->getActiveSheet()->toArray(null, true, true, true);
        if ($this->setFirstRecordAsKeys) {
            $sheetData = $this->executeArrayLabel($sheetData);
        }
        if ($indexed !== false) {
            if (!empty($this->getOnlyRecordByIndex) && isset($this->getOnlyRecordByIndex[$indexed]) && is_array($this->getOnlyRecordByIndex[$indexed])) {
                $sheetData = $this->executeGetOnlyRecords($sheetData, $this->getOnlyRecordByIndex[$indexed]);
            }
            if (!empty($this->leaveRecordByIndex) && isset($this->leaveRecordByIndex[$indexed]) && is_array($this->leaveRecordByIndex[$indexed])) {
                $sheetData = $this->executeLeaveRecords($sheetData, $this->leaveRecordByIndex[$indexed]);
            }
        } else {
            if (!empty($this->getOnlyRecordByIndex)) {
                $sheetData = $this->executeGetOnlyRecords($sheetData, $this->getOnlyRecordByIndex);
            }
            if (!empty($this->leaveRecordByIndex)) {
                $sheetData = $this->executeLeaveRecords($sheetData, $this->leaveRecordByIndex);
            }
        }
        return $sheetData;
    }

}
